#! /usr/bin/env python

import os, sys
import pygame
from pygame.locals import *
from helpers import *

class Game:

    def __init__(self, screen):
        self.screen = screen

    def start(self):

        self.screen.set_clip(self.screen.get_rect())
        self.background = pygame.Surface(self.screen.get_size())
        self.background = self.background.convert()
        self.background.fill((0,0,0))
        self.screen.blit(self.background, (0,0))

        self.hit_points = ((94,266), (300, 147), (12,99), (342,332), (183, 406))
        self.hits = []
        for x in range(len(self.hit_points)):
            self.hits.append(False)

        self.misses = 0
        self.photo_right = Photo("balls.JPG", Rect(40, 40, 450, 450))
        self.photo_left = Photo("balls.mod.JPG", Rect(534, 40, 450, 450))
        self.photo_sprites = pygame.sprite.Group(self.photo_right, self.photo_left)
        self.hit_sprites = pygame.sprite.Group()

        self.circle = load_image("circle.png", -1)
        self.photo_sprites.draw(self.screen)
        self.print_status()

        pygame.display.flip()

        while 1:
            event = pygame.event.wait()
            if event.type == pygame.QUIT or (event.type == KEYDOWN and event.key == pygame.K_q):
                return 1
            elif event.type == MOUSEBUTTONDOWN:
                self.process_mouse_click(pygame.mouse.get_pos())


            pygame.display.flip()

    def process_mouse_click(self, pos):
        clicked_photo = False
        hit = False
        for photo_ind in range(len(self.photo_sprites.sprites())):
            photo_orig = self.photo_sprites.sprites()[photo_ind]
            if photo_orig.rect.collidepoint(pos[0], pos[1]):
                clicked_photo = True
                rel_x = pos[0] - photo_orig.rect.left
                rel_y = pos[1] - photo_orig.rect.top
                for point_x in range(len(self.hit_points)):
                    point = self.hit_points[point_x]
                    rec = Rect(rel_x - 50, rel_y - 50, 100, 100)
                    if rec.collidepoint(point[0], point[1]) and not(self.hits[point_x]):
                        self.hits[point_x] = True
                        hit = True
                        self.register_hit(point, photo_ind, photo_orig)
                break
        if clicked_photo and not(hit):
            self.misses = self.misses + 1
        self.print_status()
        print "Hits: ", len(self.hit_sprites) /2
        print "Misses: ", self.misses

    def has_won(self):
        return len(self.hit_sprites) / 2 == 5

    def print_status(self):
        self.screen.set_clip(Rect(70, 500, 600, 200))
        self.screen.blit(self.background, (70, 500))
        font = pygame.font.Font(None, 48)
        msg = "Hits: %d" % (len(self.hit_sprites) / 2)
        if self.has_won():
            msg = "You won!"
        text = font.render(msg, 1, (0,255,0))
        self.screen.blit(text, (200, 600))

    def register_hit(self, point, photo_ind, photo_orig):

        rec = Rect(point[0] - 50, point[1] - 50, 100, 100)
        hit_orig = Hit(Rect(photo_orig.rect.left + rec.left, photo_orig.rect.top + rec.top, 100, 100))
        photo_mirror = self.photo_sprites.sprites()[photo_ind - 1]

        hit_mirror = Hit(Rect(photo_mirror.rect.left + rec.left, photo_mirror.rect.top + rec.top, 100, 100))
        self.hit_sprites.add(hit_orig)
        self.hit_sprites.add(hit_mirror)

        self.screen.set_clip(photo_orig.rect)
        self.hit_sprites.draw(self.screen)
        self.screen.set_clip(photo_mirror.rect)
        self.hit_sprites.draw(self.screen)

class Photo(pygame.sprite.Sprite):

    def __init__(self, filename, rect=None):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image(filename)
        if rect != None:
            self.rect = rect

class Hit(pygame.sprite.Sprite):

    def __init__(self, rect=None):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image("circle.png", -1)
        if rect != None:
            self.rect = rect
